package com.feihong.yw189.activity.kuandai;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.activity.dingdan.DingDanDetailsActivity;
import com.feihong.yw189.activity.kaika.KaiKaActivity;
import com.feihong.yw189.adapter.Kuandai.PhoneNumberAdapter;
import com.feihong.yw189.app.App;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.Kaika.PhoneNumberModel;
import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 选手机号码
 * 宽带 待删除
 */

public class PhoneNumberActivity extends BaseActivity {

    @BindView(R.id.rl_goback)
    RelativeLayout ivBack;
    @BindView(R.id.iv_lin)
    ImageView ivLin;
    @BindView(R.id.txt_pagetitle)
    TextView txtPagetitle;
    @BindView(R.id.et_input)
    EditText minput;
    @BindView(R.id.pull_recycler_view)
    PullRecyclerView mPullRecyclerView;
    @BindView(R.id.iv_search)
    ImageView  ivSubmit;

    PhoneNumberAdapter mAdapter;
    private int page = 1;
    final int page_size = 10;
    List<PhoneNumberModel.DataBean.ListBean>  mlistBean;
    private String title;


    /**
     * 套餐组id
     */
    private String mPackageGroupID;
    private String detailactivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choosesim);
        ButterKnife.bind(this);

        initView();
        initData();
        bindData();
    }

    @Override
    public void setTheme() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    private void initView() {
        mPackageGroupID = getIntent().getStringExtra("groupID");
        detailactivity = getIntent().getStringExtra("position");

        txtPagetitle.setText("手机号码");
        ivLin.setVisibility(View.GONE);
        minput.setHint("请输入搜索号码尾部");
        title = minput.getText().toString().trim();
        mlistBean = new ArrayList<>();
        // 初始化PullRecyclerView
        mPullRecyclerView.setLayoutManager(new XLinearLayoutManager(this));
        mAdapter = new PhoneNumberAdapter(this, R.layout.item_phonenum, mlistBean);


        mAdapter.setOnItemClickListener(new PhoneNumberAdapter.OnItemClickListener() {
            @Override
            public void setOnItemCklickListener(String phone,String num_lev_memo,String type_phone,String phoneId) {
                if(mPhoneMap.containsKey(phone)){
                    ToastUtils.showLong("此号码已选择，请选择其他号码");
                }else {
                    if(!TextUtils.isEmpty(detailactivity)){
                        Intent intentPhone = new Intent(PhoneNumberActivity.this,DingDanDetailsActivity.class);
                        intentPhone.putExtra("phoneNum",phone);
                        intentPhone.putExtra("num_lev_memo",num_lev_memo);
                        intentPhone.putExtra("type_phone",type_phone);
                        setResult(RESULT_OK,intentPhone);
                        finish();
                    }else{
                        Intent intentPhone = new Intent(PhoneNumberActivity.this,KaiKaActivity.class);
                        intentPhone.putExtra("phoneNum",phone);
                        intentPhone.putExtra("num_lev_memo",num_lev_memo);
                        intentPhone.putExtra("type_phone",type_phone);
                        setResult(RESULT_OK,intentPhone);
                        finish();
                    }

                }
            }

            @Override
            public void setOnItemClickListener(int position) {

            }
        });
        mPullRecyclerView.setAdapter(mAdapter);

        mPullRecyclerView.setColorSchemeResources(R.color.appBlue); // 设置下拉刷新的旋转圆圈的颜色
        mPullRecyclerView.enablePullRefresh(true); // 开启下拉刷新，默认即为true，可不用设置
        mPullRecyclerView.enableLoadDoneTip(true, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        //刷新监听
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {

            @Override
            public void onPullRefresh() {

                mPullRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        App.getApp().getHttpTools().getPhone(mPackageGroupID,title, page,page_size, CallBackUp);
                    }
                },1500 );
            }

            @Override
            public void onLoadMore() {

                // 模拟上拉加载更多网络请求
                mPullRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                    /*    mlistBean.addAll(mlistBean);
                        mAdapter.notifyDataSetChanged();
                        mPullRecyclerView.stopLoadMore();
                        mPullRecyclerView.enableLoadMore(pageSize > 0);*/
                        page++;
                        App.getApp().getHttpTools().getPhone(mPackageGroupID,title, page,page_size, CallBackUp);
                    }
                }, 1500);
            }
        });
        mPullRecyclerView.postRefreshing();
    }
    private HashMap<String,String> mPhoneMap ;
    private void initData() {
        mPhoneMap = new HashMap<String,String>();
        final Intent intent = getIntent();
        ArrayList<String> arrayList = intent.getStringArrayListExtra("phoneList");
        if(arrayList != null){
            for(String phone : arrayList){
                mPhoneMap.put(phone,phone);
            }
        }
    }

    public  void bindData(){

    }

    @OnClick({R.id.rl_goback,R.id.iv_search})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.rl_goback:
                finish();
                break;
            case R.id.iv_search:
                initView();
                break;
        }
    }

    RequestCallBack<String> CallBackUp = new RequestCallBack<String>(){
        @Override
        public void onSuccess(String result) {
            try{
                PhoneNumberModel  chooseSimModel= JSON.parseObject( result, PhoneNumberModel.class);
                 if(chooseSimModel != null){
                     if(chooseSimModel.getStatus() == 1){
                         //list解析
                         if(chooseSimModel.getData().getList().size() >0){
                             PhoneNumberModel.DataBean.ListBean listBean;
                             for( PhoneNumberModel.DataBean.ListBean  Listbean1 :chooseSimModel.getData().getList() ){
                                 listBean =new PhoneNumberModel.DataBean.ListBean();
                                 //手机号
                                 listBean.setPhonenum(Listbean1.getPhonenum());
                                 //等级
                                 listBean.setNum_lev_memo(Listbean1.getNum_lev_memo());
                                 //预存
                                 listBean.setReal_price(Listbean1.getReal_price());
                                 //低消
                                 listBean.setMini_cost(Listbean1.getMini_cost());
                                 //乌鲁木齐电信
                                 listBean.setRegion_name(Listbean1.getRegion_name());
                                 //类型
                                 listBean.setType_phone(Listbean1.getType_phone());
                                 mlistBean.add(listBean);
                             }
                             mAdapter.notifyDataSetChanged();
                             mPullRecyclerView.stopRefresh();
                             // 当剩余还有大于0页的数据时，开启上拉加载更多
                             mPullRecyclerView.enableLoadMore(page > 0);
                         }else{
                             showErrorToast(TAG+ "没有数据");
                         }
                     }else{
                         showErrorToast(TAG + "状态: " + chooseSimModel.getInfo());
                     }
                 }else{
                     showErrorToast(TAG+ "解析出错,请重试");
                 }
            }catch (Exception ex){
                showErrorToast(TAG+ "信息出错了,请稍后重试～");
            }
        }
        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }
        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }
        @Override
        public void onFinished() {

        }
    };

}
